// pages/calendar/calendar.js
var util = require('../../utils/util.js')
var app = getApp();
var key = app.globalData.key;
var resurl = app.globalData.resurl
let interstitialAd = null;
let videoAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDelay: '',
    videoMoney: '',
    isReview1: '',
    isReview:  '',
    isDraw: '',
    switchDouble:'',
    normal: false,
    normalMask: false,
    bagOne:'',
    bagTwo:'',
    bagThree:'',
    videoShow: false,
    adverShow: true,
    moneyWan: '',
    timeyuanbao: '',
    doubleMoney: '',
    userMoney: '',
    randomMoney: '',
    trueMoney: '',
    touxiang: '/images/avatar_login.png',
    loginName: '游客',
    money: '',
    month: 0,
    date: 0,
    year: 0,
    redbagTime1: '',
    redbagTime2: '',
    redbagTime3: '',
    oneMinutesRest: '',
    fiveMinutesRest: '',
    threeMinutesRest: '',
    oneShow: '',
    twoShow: false,
    thrShow: false,
    timeShow: false,
    showMask: '',
    showMaskTwo: false,
    showMaskNext: false,
    opacity: 0.5,
    toastSrc: '/images/bg_new_user_1.png',
    toastSrcopend: '/images/bg_new_user_2.png',
    yearShengXiao: '',
    monthInGanZhi: '',
    yearInGanZhi: '',
    season: '',
    dayYi: '',
    dayJi: '',
    isNew: '',
    flag: 0,
  },

  restTimerI: '',
  restTimerIII: '',
  restTimerV: '',
  videoAD_time: '',
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function () {
    var that = this;
    var isDelay = wx.getStorageSync("isDelay");
    var videoMoney = wx.getStorageSync("videoMoney");
    that.setData({
      isDelay,
      videoMoney,

    })
    var now = new Date();
    // var newLimit = now.getTime();
    // wx.setStorageSync("newLimit",newLimit);
    var userId = wx.getStorageSync("userId");
    console.log(userId);
    if (userId == "") {
      //登录
      wx.login({
        success(res) {
          if (res.code) {
            //发起网络请求
            var _data = {
              deviceProperties: {
                key: res.code,
                version: '1.0.1',
                appId: 'wxa8472e39b0ca16a7',
              }
            }
            _data = JSON.stringify(_data);
            _data = util.base64_encode(_data);
            _data = util.xxtea_encrypt(_data, key);
            wx.request({
              url: resurl + '/interface/sdkData?requestId=1&a=1&b=2',
              data: _data,
              header: {
                "content-type": "application/json",
                "dataType": "json",
              },
              method: "post",
              success: function (res) {
                res = util.xxtea_decrypt(res.data, key);
                res = util.base64_decode(res);
                res = JSON.parse(res);
                console.log("接口1" + res.data);
                if (res.resultCode === 0) {
                  res = JSON.parse(res.data);
                  console.log(res);
                  if (res.user.isNew === 0) {
                    that.setData({
                      oneShow: true,
                      showMask: true,
                      randomMoney: res.user.money,
                    })
                  } else {
                    console.log('我是老用户');
                  }
                  wx.setStorageSync("userId", res.user.userId);
                  wx.setStorageSync("isNew", res.user.isNew);
                  var loginName = unescape(res.user.realName);
                  var trueMoney = Math.floor(res.user.money / 10000 * 100) / 100
                  that.setData({
                    monthInGanZhi: res.monthInGanZhi,
                    yearShengXiao: res.yearShengXiao,
                    yearInGanZhi: res.yearInGanZhi,
                    season: res.season,
                    money: res.user.money,
                    trueMoney: trueMoney,
                    touxiang: res.user.userLogo,
                    loginName: loginName,
                    dayYi: res.dayYi,
                    dayJi: res.dayJi,
                  })
                  app.globalData.money = res.user.money;
                  var money = res.user.money;
                  if (money < 10000) {
                    that.setData({
                      moneyWan: money,
                    })
                  }
                  else if (money >= 10000) {
                    var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
                    console.log(moneyWan);
                    that.setData({
                      moneyWan: moneyWan,
                    })
                  }
                  // 1分钟
                  if (res.redbagTime1 === 0 || res.redbagTime1 === undefined) {
                    that.setData({
                      oneMinutesRest: '可领取'
                    })
                  }
                  // 三分钟
                  if (res.redbagTime2 === 0 || res.redbagTime2 === undefined) {
                    that.setData({
                      threeMinutesRest: '可领取'
                    })
                  }
                  // 五分钟
                  if (res.redbagTime3 === 0 || res.redbagTime3 === undefined) {
                    that.setData({
                      fiveMinutesRest: '可领取'
                    })
                  }
                }
              }
            })
          }
          else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    } else {

    }
    // let now = new Date();
    let month = now.getMonth();
    let date = now.getDate();
    let year = now.getFullYear();
    that.setData({
      month: month + 1,
      date: date,
      year: year,
      flag: 1
    })
  },
  insertScreenAD: function () {
    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-1cc4cb154d00aa08'
      })
      interstitialAd.onLoad(() => { })
      interstitialAd.onError((err) => { })
      interstitialAd.onClose(() => { })
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  //看视频翻倍
  doubleYuanbao: function (bagId) {
    var that = this;
    var money = that.data.money;
    var data6 = {
      deviceProperties: app.globalData.deviceProperties,
      gainRedBag: {
        id: bagId,
      }
    }
    data6 = JSON.stringify(data6);
    data6 = util.base64_encode(data6);
    data6 = util.xxtea_encrypt(data6, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=6&a=1&b=2',
      data: data6,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res.data + "我是翻倍的金额");
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          that.normalToast(res.getMoney)
          var money = that.data.money;
          if (money < 10000) {
            that.setData({
              moneyWan: money,
            })
          }
          else if (money >= 10000) {
            var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
            console.log(moneyWan);
            that.setData({
              moneyWan: moneyWan,
            })
          }
          console.log(res.getMoney + "我是翻倍的金额");
          
        }
      }
    })
  },
  // 视频元宝接口领取奖励
  getVideoMoney: function () {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var money = that.data.money;
    var _data5 = {
      deviceProperties: app.globalData.deviceProperties,
      user:{
        userId:userId,
      }
    }
    _data5 = JSON.stringify(_data5);
    _data5 = util.base64_encode(_data5);
    _data5 = util.xxtea_encrypt(_data5, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=5&a=1&b=2',
      data:_data5,
      header:{
        "content-type":"application/json",
        "dataType":"json",
      },
      method:"POST",
      success:function(res){
        res = util.xxtea_decrypt(res.data,key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          that.normalToast(res.getMoney);
          var money = that.data.money;
          if (money < 10000) {
            that.setData({
              moneyWan: money,
            })
          }
          else if (money >= 10000) {
            var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
            console.log(moneyWan);
            that.setData({
              moneyWan: moneyWan,
            })
          }
          console.log(res.getMoney);
        }
      }
          })
  },
  // 双倍开关
  switchVideoDouble:function (bagId) {
    var that = this ;
    var userId = wx.getStorageSync("userId");
    var _data5 = {
      deviceProperties: app.globalData.deviceProperties,
      user:{
        userId:userId,
      }
    }
    _data5 = JSON.stringify(_data5);
    _data5 = util.base64_encode(_data5);
    _data5 = util.xxtea_encrypt(_data5, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=0&a=1&b=2',
      data:_data5,
      header:{
        "content-type":"application/json",
        "dataType":"json",
      },
      method:"POST",
      success:function(res){
        res = util.xxtea_decrypt(res.data,key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          if(res.isDraw == '1') {
            // 开关返回1 开启视频广告
            that.encourageVideoADDouble(bagId);
          } else {
            wx.showModal({
              title: "让眼睛休息会吧",
              shwoCancel: false,
            })
          }
        }
      }
          })
    
  },
  // 显示领取元宝奖励的弹窗
  timeToast: function (a) {
    var that = this;
    var money = that.data.money;
    app.globalData.money = money + a;
    that.setData({
      timeShow: true,
      showMaskNext: true,
      money: app.globalData.money,
      timeyuanbao: a,
    })
    var userMoney = Math.floor(app.globalData.money / 10000 * 100) / 100;
    that.setData({
      userMoney: userMoney,
    })

  },
  normalToast: function (a) {
    var that = this;
    var money = that.data.money;
    app.globalData.money = money + a;
    that.setData({
      normal: true,
      normalMask: true,
      money: app.globalData.money,
      normalyuanbao: a,
    })
    var userMoney = Math.floor(app.globalData.money / 10000 * 100) / 100;
    that.setData({
      userMoney: userMoney,
    })
  },
  //回到今天
  returnday: function (e) {
    var that = this;
    var now = new Date();
    var date = now.getDate();
    var month = now.getMonth();
    var year = now.getFullYear();
    that.setData({
      date: date,
      month: month + 1,
      year: year
    })
    var calendar = year + ',' + (month + 1) + ',' + date;
    that.getUserInfo(calendar);
  },
  // 日期往前
  discrease: function (e) {
    var that = this;
    var date = e.currentTarget.dataset.date;
    var month = e.currentTarget.dataset.month;
    var year = e.currentTarget.dataset.year;
    var arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (year % 4 === 0 && year % 400 !== 0) {
      arr[1] = 29
    } else {
      arr[1] = 28
    }
    if (date > 1) {
      that.setData({
        date: date - 1,
      })
      var calendar = year + ',' + month + ',' + (date - 1);
    }
    else {
      that.setData({
        date: arr[month - 2],
        month: month - 1,
      })
      var calendar = year + ',' + (month - 1) + ',' + arr[month - 2];
    }
    if (month === 1 && date === 1) {
      that.setData({
        month: 12,
        year: year - 1,
        date: arr[month - 1],
      })
      var calendar = (year - 1) + ',' + '12' + ',' + '31';
    }
    console.log(calendar);
    that.getCalendar(calendar);
  },
  // 日期往后
  increase: function (e) {
    var that = this;
    var date = e.currentTarget.dataset.date;
    var month = e.currentTarget.dataset.month;
    var year = e.currentTarget.dataset.year;
    var arr = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if (year % 4 === 0 && year % 400 !== 0) {
      arr[1] = 29
    } else {
      arr[1] = 28
    }
    if (date < arr[month - 1]) {
      that.setData({
        date: date + 1,
      })
      var calendar = year + ',' + month + ',' + (date + 1);
    }
    else {
      that.setData({
        date: 1,
        month: month + 1
      })
      var calendar = year + ',' + (month + 1) + ',' + 1;
    }
    if (month === 12 && date === 31) {
      that.setData({
        month: 1,
        year: year + 1,
        date: 1
      })
      var calendar = (year + 1) + ',' + '1' + ',' + '1';
    }
    console.log(calendar);
    that.getCalendar(calendar);
  },
  getCalendar: function (calendar) {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        createTime: calendar,
      }
    }
    console.log(_data)
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    //获取用户信息接口
    wx.request({
      url: resurl + '/interface/sdkData?requestId=3&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);//可查看请求失败信息。。。。
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          that.setData({
            monthInGanZhi: res.monthInGanZhi,
            yearShengXiao: res.yearShengXiao,
            yearInGanZhi: res.yearInGanZhi,
            season: res.season,
            dayYi: res.dayYi,
            dayJi: res.dayJi,
          })
        }
      }
    })
  },
  // 打开新用户奖励
  open: function () {
    var _this = this;
    _this.setData({
      oneShow: false,
      twoShow: true,
    })
  },
  // 开启赚钱之旅
  getprofit: function () {
    var _this = this;
    _this.setData({
      oneShow: false,
      twoShow: false,
      showMask: false,
    })
  },
  // 提现
  quicktixian: function () {
    wx.navigateTo({
      url: '/pages/getMoney/getMoney'
    })
  },
  clickclose: function () {
    this.setData({
      thrShow: false,
      showMaskTwo: false
    })
  },
  clickclosefour: function () {
    var that = this;
    var isDelay = that.data.isDelay;
    if(isDelay == 1) {
      var delayTimer = setInterval(function () {
        that.setData({
          timeShow: false,
          showMaskNext: false,
          normal: false,
          normalMask: false,
        })
        clearInterval(delayTimer);
      },2000)
    } else {
      that.setData({
        timeShow: false,
        showMaskNext: false,
        normal: false,
        normalMask: false,
      })
    }
  },
  // 点击翻倍 调用视频接口
  doubled: function (e) {
    var bagOne = e.currentTarget.dataset.bagone;
    var bagTwo = e.currentTarget.dataset.bagtwo;
    var bagThree = e.currentTarget.dataset.bagthree;
    console.log(bagOne);
    console.log(bagTwo);
    console.log(bagThree);
    var that = this;
    that.setData({
      timeShow: false,
      showMaskNext: false,
    })
    if(bagOne) {
      that.switchVideoDouble(bagOne);
    }
    if(bagTwo) {
      that.switchVideoDouble(bagTwo);
    }
    if(bagThree) {
      that.switchVideoDouble(bagThree);
    }
    
  },

  // 一分钟定时元宝
  oneMinutes: function (e) {
    var that = this;
    var bagType = 1;
    that.getTimeYuanbaoOne(bagType);//领取元宝接口
  },
  // 定时器1分钟
  setTimerI: function (a) {
    var that = this;
    var countTime = a;
    console.log(a);
    clearInterval(that.restTimerI);
    var restTimerI = setInterval(function () {
      if (countTime > 10) {
        that.setData({
          oneMinutesRest: '00:00:' + --countTime,
        })
      }
      // console.log(countTime)
      else {
        that.setData({
          oneMinutesRest: '00:00:0' + --countTime,
        })
      }
      if (countTime < 1) {
        that.setData({
          oneMinutesRest: '可领取'
        })
        clearInterval(restTimerI);
      }
    }, 1000)
    that.restTimerI = restTimerI;
  },
  // 三分钟
  threeMinutes: function () {
    var that = this;
    var bagType = 2;
    that.getTimeYuanbaoTwo(bagType);//领取元宝接口
  },
  // 三分钟元宝定时器
  setTimerIII: function (b) {
    var that = this;
    clearInterval(that.restTimerIII);
    var m = Math.floor(b / 60);
    var s = b % 60;
    console.log(m);
    console.log(s);
    var restTimerIII = setInterval(function () {
      if (s === 0 && m != 0) {
        m = m - 1;
        s = 60;
        if (s > 10) {
          that.setData({
            threeMinutesRest: '00:0' + m + ':' + --s,
          })
        } else {
          that.setData({
            threeMinutesRest: '00:0' + m + ':0' + --s,
          })
        }
      } else if (m === 0 && s !== 0) {
        if (s > 10) {
          that.setData({
            threeMinutesRest: '00:0' + m + ':' + --s,
          })
        } else {
          that.setData({
            threeMinutesRest: '00:0' + m + ':0' + --s,
          })
        } if (s < 1) {
          that.setData({
            threeMinutesRest: '可领取'
          })
          clearInterval(restTimerIII);
        }
      }
      else if (m !== 0 && s !== 0) {
        if (s > 10) {
          that.setData({
            threeMinutesRest: '00:0' + m + ':' + --s,
          })
        } else {
          that.setData({
            threeMinutesRest: '00:0' + m + ':0' + --s,
          })
        }
      }
      // else if (s < 1 && m === 0) {
      //   that.setData({
      //     threeMinutesRest: '可领取'
      //   })

      // }
    }, 1000)
    that.restTimerIII = restTimerIII;
  },
  // 五分钟
  fiveMinutes: function () {
    var that = this;
    var bagType = 3;
    that.getTimeYuanbaoThree(bagType);//领取元宝接口
  },
  // 五分钟元宝定时器
  setTimerV: function (c) {
    var that = this;
    clearInterval(that.restTimerV);
    var m = Math.floor(c / 60);
    var s = c % 60;
    var restTimerV = setInterval(function () {
      if (s === 0 && m !== 0) {
        m = m - 1;
        s = 60;
        if (s > 10) {
          that.setData({
            fiveMinutesRest: '00:0' + m + ':' + --s,
          })
        } else {
          that.setData({
            fiveMinutesRest: '00:0' + m + ':0' + --s,
          })
        }
      } else if (m === 0 && s !== 0) {
        if (s > 10) {
          that.setData({
            fiveMinutesRest: '00:0' + m + ':' + --s,
          })
        } else {
          that.setData({
            fiveMinutesRest: '00:0' + m + ':0' + --s,
          })
        }
        if (s < 1) {
          that.setData({
            fiveMinutesRest: '可领取'
          })
          clearInterval(restTimerV);
        }
      } else if (m !== 0 && s !== 0) {
        if (s > 10) {
          that.setData({
            fiveMinutesRest: '00:0' + m + ':' + --s,
          })
        } else {
          that.setData({
            fiveMinutesRest: '00:0' + m + ':0' + --s,
          })
        }
      }
    }, 1000)
    that.restTimerV = restTimerV;
  },

  //领取定时元宝接口 4
  getTimeYuanbaoOne: function (bagType) {
    var that = this;
    var videoMoney = that.data.videoMoney;
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        bagType: bagType,
      }
    }
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=4&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        // console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          // console.log(res);
          that.setData({
            bagOne: res.bagId,
          })
          console.log(that.data.bagOne);
          var money = that.data.money;
          if (money < 10000) {
            that.setData({
              moneyWan: money,
            })
          }
          else if (money >= 10000) {
            var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
            console.log(moneyWan);
            that.setData({
              moneyWan: moneyWan,
            })
          }
          if(money > videoMoney) {
            console.log("为什么不弹窗")
            that.timeToast(res.getMoney);//有双倍奖励，可开启视频的弹窗
          } else {
            console.log("为什么不弹窗")
            that.normalToast(res.getMoney);//没有可开启视频的弹窗
          }
          that.setTimerI(60);
          // console.log(res);
        } else if (res.resultCode === -9) {
          res = JSON.parse(res.data);
          console.log(res);
          console.log(res.betNum);
          var a = res.betNum; //剩余时间
          console.log(a);
          that.setTimerI(a);
          wx.showModal({
            title: "剩余时间为：" + res.betNum + 's',
            shwoCancel: false,
            content: '请稍后再试！',
          })
        }
      }
    })
  },
  //领取定时元宝接口 4
  getTimeYuanbaoTwo: function (bagType) {
    var that = this;
    var videoMoney = that.data.videoMoney;
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        bagType: bagType,
      }
    }
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=4&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        // console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          //  console.log(res);
           that.setData({
            bagTwo: res.bagId,
          })
          console.log(that.data.bagTwo);
          var money = that.data.money;
          if (money < 10000) {
            that.setData({
              moneyWan: money,
            })
          }
          else if (money >= 10000) {
            var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
            console.log(moneyWan);
            that.setData({
              moneyWan: moneyWan,
            })
          }
          if(money > videoMoney) {
            that.timeToast(res.getMoney);
          } else {
            that.normalToast(res.getMoney);
          }
          that.setTimerIII(180);
          // console.log(res);
          // console.log(res.getMoney);
        } else if (res.resultCode === -9) {
          res = JSON.parse(res.data);
          that.setTimerIII(res.betNum);
          console.log("剩余时间" + res.betNum);
          wx.showModal({
            title: "剩余时间为：" + res.betNum + 's',
            shwoCancel: false,
            content: '请稍后再试！',
          })
        }
      }
    })
  },
  //领取定时元宝接口 4
  getTimeYuanbaoThree: function (bagType) {
    var that = this;
    var videoMoney = that.data.videoMoney;
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        bagType: bagType,
      }
    }
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=4&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          // console.log(res);
          that.setData({
            bagThree: res.bagId
          })
          console.log(that.data.bagThree);
          var money = that.data.money;
          if (money < 10000) {
            that.setData({
              moneyWan: money,
            })
          }
          else if (money >= 10000) {
            var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
            console.log(moneyWan);
            that.setData({
              moneyWan: moneyWan,
            })
          }
          if(money > videoMoney) {
            that.timeToast(res.getMoney);
          } else {
            that.normalToast(res.getMoney);
          }
          that.setTimerV(300);
          // console.log(res);
          // console.log(res.getMoney);
        } else if (res.resultCode === -9) {
          res = JSON.parse(res.data);
          that.setTimerV(res.betNum);
          console.log("剩余时间" + res.betNum);
          wx.showModal({
            title: "剩余时间为：" + res.betNum + 's',
            shwoCancel: false,
            content: '请稍后再试！',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  // 上次版本需要的审核号
  getVersion: function () {
    var that = this ;
    var userId = wx.getStorageSync("userId");
    var _data1 = {
      deviceProperties: app.globalData.deviceProperties,
      user:{
        userId:userId,
      }
    }
    _data1 = JSON.stringify(_data1);
    _data1 = util.base64_encode(_data1);
    _data1 = util.xxtea_encrypt(_data1, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=0&a=1&b=2',
      data:_data1,
      header:{
        "content-type":"application/json",
        "dataType":"json",
      },
      method:"POST",
      success:function(res){
        res = util.xxtea_decrypt(res.data,key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          wx.setStorageSync("isDelay",res.isDelay);
          wx.setStorageSync("isReview",res.isReview);
          wx.setStorageSync("isReview1",res.isReview1);
          wx.setStorageSync('videoMoney', res.videoMoney)
          that.setData({
            isDraw: res.isDraw,
            switchDouble: res.isAd,
            isReview: res.isReview,
            isReview1: res.isReview1,
            videoMoney: res.videoMoney,
          })
        }
      }
          })
    
  },
  onShow: function () {
    var that = this;
    var date = that.data.date;
    var month = that.data.month;
    var year = that.data.year;
    var calendar = year + ',' + month + ',' + date;
    that.getUserInfo(calendar);
    that.getVersion();
    that.encourageVideoAD();
    var now = new Date();
    var count = now.getTime();
    var newLimit = wx.getStorageSync("newLimit");
    var insertScreenAdTimer = setInterval(function () {
      if( count > newLimit ) {
        that.insertScreenAD();
        clearInterval(insertScreenAdTimer);
      }
    },1000)
    that.insertScreenAdTimer = insertScreenAdTimer;
  },
  // 获取用户信息
  getUserInfo: function (calendar) {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        createTime: calendar,
      }
    }
    console.log(_data)
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    //获取用户信息接口
    wx.request({
      url: resurl + '/interface/sdkData?requestId=3&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);//可查看请求失败信息。。。。
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          var app = getApp();
          app.globalData.money = res.user.money;
          that.setData({
            money: app.globalData.money,
          })
          that.setData({
            monthInGanZhi: res.monthInGanZhi,
            yearShengXiao: res.yearShengXiao,
            yearInGanZhi: res.yearInGanZhi,
            season: res.season,
            dayYi: res.dayYi,
            dayJi: res.dayJi,
            money: res.user.money,
            touxiang: res.user.userLogo,
            loginName: res.user.realName,
            isNew: res.user.isNew,
          })
          var money = that.data.money;
          if (money < 10000) {
            that.setData({
              moneyWan: money,
            })
          }
          else if (money >= 10000) {
            var moneyWan = Math.floor(money / 10000 * 100) / 100 + 'w';
            console.log(moneyWan);
            that.setData({
              moneyWan: moneyWan,
            })
          }
          // 元宝一分钟
          if (res.redbagTime1) {
            clearInterval(that.restTimerI);
            var a = res.redbagTime1
            that.setTimerI(a);
          } else if (res.redbagTime1 === 0) {
            that.setData({
              oneMinutesRest: '可领取'
            })
          }
          // 三分钟
          if (res.redbagTime2) {
            clearInterval(that.restTimerIII);
            that.setTimerIII(res.redbagTime2);
          } else if (res.redbagTime2 === 0) {
            that.setData({
              threeMinutesRest: '可领取'
            })
          }
          // 五分钟
          if (res.redbagTime3) {
            clearInterval(that.restTimerV);
            that.setTimerV(res.redbagTime3);
          } else if (res.redbagTime3 === 0) {
            that.setData({
              fiveMinutesRest: '可领取'
            })
          }
        }
      }
    })
  },
  // 激励视频广告  给视频元宝用的
  encourageVideoAD: function () {
    // 在页面中定义激励视频广告
    var that = this;
    

    // 在页面onLoad回调事件中创建激励视频广告实例
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-9b265556b1d319a5'
      })
      videoAd.onLoad(() => { })
      videoAd.onError((err) => { })
      videoAd.onClose((res) => { 
        if (res && res.isEnded || res === undefined) {
          console.log("我看完视频了");
          that.getVideoMoney();
        } else {
          // 播放中途退出，进行提示
          wx.showToast({
            title: '观看广告时间不足，无法获得奖励',
            icon: "none",
            duration: 2000
          })
        }

      })
    }
   
  },
  openVideo: function () {
     // 用户触发广告后，显示激励视频广告
     if (videoAd) {
      videoAd.show().catch(() => {
        // 失败重试
        videoAd.load()
          .then(() => videoAd.show())
          .catch(err => {
            console.log('激励视频 广告显示失败')
          })
      })
    }
    console.log("看广告")
  },
  // 给翻倍奖励用的 点击弹窗的double
  encourageVideoADDouble: function (bagId) {
    // 在页面中定义激励视频广告
    var that = this;

    // 在页面onLoad回调事件中创建激励视频广告实例
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-9b265556b1d319a5'
      })
      videoAd.onLoad(() => { })
      videoAd.onError((err) => { })
      videoAd.onClose((res) => { 
        if (res && res.isEnded || res === undefined) {
          console.log("我看完视频了");
          // 调用翻倍接口6
          that.doubleYuanbao(bagId);
        } else {
          // 播放中途退出，进行提示
          wx.showToast({
            title: '观看广告时间不足，无法获得奖励',
            icon: "none",
            duration: 2000
          })
        }

      })
    }

    // 用户触发广告后，显示激励视频广告
    if (videoAd) {
      videoAd.show().catch(() => {
        // 失败重试
        videoAd.load()
          .then(() => videoAd.show())
          .catch(err => {
            console.log('激励视频 广告显示失败')
          })
      })
    }
  },
  // 关闭banner广告
  adClose: function () {
    this.setData({
      adverShow: false,
      videoShow: true,
    })
  },
  videoClose: function () {
    this.setData({
      videoShow: false,
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    clearInterval(that.restTimerI);
    clearInterval(that.restTimerIII);
    clearInterval(that.restTimerV);
    clearInterval(that.insertScreenAdTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.restTimerI);
    clearInterval(that.restTimerIII);
    clearInterval(that.restTimerV);
    clearInterval(that.insertScreenAdTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})